<?php
declare (strict_types = 1);

namespace app\controller;

use AlibabaCloud\SDK\ICE\V20201109\ICE;
use AlibabaCloud\Credentials\Credential;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\ICE\V20201109\Models\SubmitBatchMediaProducingJobRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use think\Request;
use think\Response;

class AliMedia
{
    protected $client;

    /**
     * 构造函数，初始化 ICE 客户端
     */
    public function __construct()
    {
        $this->client = $this->createClient();
    }

    /**
     * 初始化 ICE 客户端
     * @return ICE
     */
    public function createClient()
    {
        $credential = new Credential([
            'type' => 'access_key',
            'accessKeyId' => 'LTAIDQzqZVNgYuy9',
            'accessKeySecret' => 'Q37wf0JMFE0VQAPfKOAD6O6MPeTuQV'
        ]);
        $config = new Config([
            "credential" => $credential
        ]);
        $config->endpoint = "ice.cn-hangzhou.aliyuncs.com";
        return new ICE($config);
    }

    /**
     * 提交批量媒体制作任务
     * @param Request $request
     * @return Response
     */
    public function submitBatchMediaProducingJob(Request $request): Response
    {
        $params = $request->post();
        $request = new SubmitBatchMediaProducingJobRequest([
            // 这里添加您的请求参数
            'inputConfig' => json_encode([
                'MediaArray' => [
                    'https://laojiushipin.oss-cn-hangzhou.aliyuncs.com/video_20240807_072716.mp4',
                    'https://laojiushipin.oss-cn-hangzhou.aliyuncs.com/1_video_20240807_072829.mp4',
                    'https://laojiushipin.oss-cn-hangzhou.aliyuncs.com/1_video_20240807_072802.mp4',
                    'https://laojiushipin.oss-cn-hangzhou.aliyuncs.com/1_video_20240807_072746.mp4',
                    'https://laojiushipin.oss-cn-hangzhou.aliyuncs.com/1_video_20240806_103000.mp4',
                    'https://laojiushipin.oss-cn-hangzhou.aliyuncs.com/1_video_20240806_102945.mp4'
                ],
                'SpeechTextArray' => [
                    '你最近是不是忙到没时间好好吃一顿饭？累到需要一口热乎又满足的美味来治愈灵魂？那你一定不能错过——黄焖鸡米饭！鲜嫩多汁的鸡肉，炖得外酥里嫩，每一口都感受到汤汁慢慢渗入肉纤维的香气；还有软糯入味的土豆块，搭配上浓郁热腾的黄焖汤汁，最后再配上一碗粒粒分明、香喷喷的白米饭——这一口下去，简直就是人间天堂！ 无论是上班赶时间，中午用餐，还是外卖到家，黄焖鸡米饭都是你的不二之选！快来解锁这一份经典国民美味，不管什么时候，都能让你元气满满、小满足大幸福！还等什么？记得下单黄焖鸡米饭，迎接一场美食的纯粹治愈吧'
                ],
                'SceneInfo' => [
                    'Scene' => 'General'
                ]
            ]),
            'editingConfig' => json_encode([
                'SpeechConfig' => [
                    'Volume' => 2,
                    'Voice' => 'loongbella_v2',
                    'SpeechRate' => 80,
                    'AsrConfig' => [
                        'EffectColorStyle' => 'CS0001-000004'
                    ],
                ],
                'BackgroundMusicConfig' => [
                    'Volume' => 0.4
                ]
            ]),
            'outputConfig' => json_encode([
                'MediaURL' => 'https://laojiushipin.oss-cn-hangzhou.aliyuncs.com/xxx_{index}.mp4',
                'Count' => '1',
                'MaxDuration' => 30,
                'Width' => 1080,
                'Height' => 1920
            ])
        ]);
        // var_dump($request);
        $runtime = new RuntimeOptions([]);
        try {
            $result = $this->client->submitBatchMediaProducingJobWithOptions($request, $runtime);
            return json(['code' => 1, 'msg' => '提交成功', 'data' => $result]);
        } catch (\Exception $error) {
            if (!($error instanceof TeaError)) {
                $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
            }
            return json(['code' => 0, 'msg' => '提交失败: ' . $error->message]);
        }
    }

    /**
     * 通过任务ID查询结果
     * @param Request $request
     * @return Response
     */
    public function getMediaProducingJobResult(Request $request): Response
    {
        $jobId = $request->param('job_id');
        if (empty($jobId)) {
            return json(['code' => 0, 'msg' => '任务ID不能为空']);
        }

        try {
            $request = new \AlibabaCloud\SDK\ICE\V20201109\Models\GetBatchMediaProducingJobRequest([
                'jobId' => $jobId
            ]);
            $runtime = new \AlibabaCloud\Tea\Utils\Utils\RuntimeOptions([]);
            $result = $this->client->getBatchMediaProducingJobWithOptions($request, $runtime);
            return json(['code' => 1, 'msg' => '查询成功', 'data' => $result]);
        } catch (\Exception $error) {
            if (!($error instanceof \AlibabaCloud\Tea\Exception\TeaError)) {
                $error = new \AlibabaCloud\Tea\Exception\TeaError([], $error->getMessage(), $error->getCode(), $error);
            }
            return json(['code' => 0, 'msg' => '查询失败: ' . $error->message]);
        }
    }
} 